import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
//换肤数据
import themeColor from "./modules/themeColor.js";
//请求的数据
import apiRequest from "@/api/request.js";
//缓存页面数据
const storeHomeData = {};
//用户信息
const SJYS_userInfo = uni.getStorageSync('userInfo') || {};
//手机信息
let pixelRatio = uni.getSystemInfoSync().pixelRatio;
console.log('pixelRatio>>>', pixelRatio);
let chartWidth = '1420rpx';
if (pixelRatio == 2) {
	chartWidth = '1420rpx';
} else if (pixelRatio == 3) {
	chartWidth = '2840rpx';
}
const phoneInfo = {
	chartWidth: chartWidth,
	client: {
		id: '',
		clientid: '',
		appid: '',
		appkey: ''
	},
	app: {},
	system: {},
	device: {
		imei: '',
		imsi: [],
		uuid: ''
	}
};

const store = new Vuex.Store({
	state: {
		userId: '',
		themeColor: {
			title: '嫣红',
			name: 'red',
			color: '#e54d42',
			index: 1
		},
		apiRequest: apiRequest,
		phoneInfo: phoneInfo,
		errorList: [],
		userInfo: SJYS_userInfo
	},

	getters: {
		getThemeColor(state) {
			let themeColor1 = state.themeColor;
			let themeColor2 = uni.getStorageSync('themeColor');
			var themeColor = { ...themeColor1,
				...themeColor2
			}
			return themeColor;
		},
		getApiRequest(state) {
			let request1 = state.apiRequest;
			let request2 = uni.getStorageSync('apiRequest');
			var request = { ...request1,
				...request2
			}
			return request;
		},
		getPhoneInfo(state) {
			let phoneInfo1 = state.phoneInfo;
			let phoneInfo2 = uni.getStorageSync('phoneInfo');
			var phoneInfo = { ...phoneInfo1,
				...phoneInfo2
			}
			return phoneInfo;
		},
		getErrorList(state) {
			let errorList1 = state.errorList;
			let errorList2 = uni.getStorageSync('errorList');
			var errorList = { ...errorList1,
				...errorList2
			}
			return errorList;
		},
		getUserInfo(state) {
			let userInfo1 = state.userInfo;
			let userInfo2 = uni.getStorageSync('userInfo');
			var userInfo = { ...userInfo1,
				...userInfo2
			}
			return userInfo;
		}
	},
	mutations: {
		setUserInfo(state, val) {
			var UserInfo = {}
			if (JSON.stringify(val) == '{}') {
				UserInfo = {};
			} else {
				let UserInfo2 = uni.getStorageSync('userInfo');
				if (UserInfo2) {
					UserInfo = { ...UserInfo2,
						...val
					}
				} else {
					UserInfo = { ...state.userInfo,
						...val
					}
				}
			}
			state.userInfo = UserInfo;
			uni.setStorageSync('userInfo', UserInfo);
		},
		//修改颜色
		setThemeColor(state, val) {
			var themeColor = {}
			if (JSON.stringify(val) == '{}') {
				themeColor = {};
			} else {
				let themeColor1 = state.themeColor;
				let themeColor2 = uni.getStorageSync('themeColor');
				themeColor = { ...themeColor1,
					...val
				}
			}
			state.themeColor = themeColor;
			uni.setStorageSync('themeColor', themeColor);
		},
		setApiRequest(state, val) {
			var request = {}
			if (JSON.stringify(val) == '{}') {
				request = {};
			} else {
				let request1 = state.apiRequest;
				let request2 = uni.getStorageSync('apiRequest');
				request = { ...request1,
					...val
				}
			}
			state.apiRequest = request;
			uni.setStorageSync('apiRequest', request);
		},
		setPhoneInfo(state, val) {
			var phoneInfo = {}
			if (JSON.stringify(val) == '{}') {
				phoneInfo = {};
			} else {
				let phoneInfo1 = state.phoneInfo;
				let phoneInfo2 = uni.getStorageSync('phoneInfo');
				phoneInfo = { ...phoneInfo1,
					...phoneInfo2
				}
				phoneInfo = { ...phoneInfo,
					...val
				}
			}
			state.phoneInfo = phoneInfo;
			uni.setStorageSync('apiRequest', phoneInfo);
		},
		setErroeList(state, val) {
			let errorList = uni.getStorageSync('errorList');

			console.log('stroe errorList1', errorList);
			if (!errorList) {
				errorList = [];
			}
			// if (errorList.length >= 50) { //只保留最近50笔
			// 	errorList.removeAt(0)
			// }
			errorList.push(val);
			//console.log('stroe errorList2',errorList);
			state.errorList = errorList;
			uni.setStorageSync('errorList', errorList);
		}
	},
	actions: {},
	modules: {

	}
})

export default store;
